package com.itboye.ihomebank.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 忘记密码3（重新输入密码）
 * 
 */
public class PasswordActivityThree extends BaseOtherActivity implements
		OnClickListener ,Observer{
	View v_statusbar;
	ImageView close_icon;// 返回图片
	TextView add_shap_title_tv;// 标题
	EditText pass_three_num;// 密码
	TextView pass_three_ok;// 完成

	private Intent intent;
	
	private String phone;//接上页面传来的手机号
	private String yzm;//接上页面传来的验证码
	
	UserPresenter userPresenter;
	
	String pass;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();

			break;
		case R.id.pass_three_ok:// 完成
			changepassword();
			break;

		default:
			break;
		}

	}
	/**
	 * 修改密码
	 */
	private void changepassword() {
		userPresenter=new UserPresenter(this);
		pass=pass_three_num.getText().toString();
		if (pass .equals("")) {
			ByAlert.alert("请输入新的密码");
			return;
		}
		userPresenter.backPass(phone, yzm, pass_three_num.getText().toString());
		
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity=handlerError(data);
		if(entity!=null){
			if(!entity.getCode().equals(0)){
				if(entity.getEventType()==UserPresenter.backPass_success){
					String bean=(String) entity.getData();
					Log.i("result", bean.toString());
					if(bean!=null){
						SPUtils.put(this,null, SPContants.PASSWORD,pass);
						ByAlert.alert("修改成功");
						finish();
						
					}
				}if(entity.getEventType() == UserPresenter.backPass_fail){
					ByAlert.alert("修改失败");
					
				}
			}
		}		
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_password_three;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("重置密码");

		intent = super.getIntent();
		phone = intent.getStringExtra("phone");
		yzm = intent.getStringExtra("yzm");
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}


}
